package com.qdxwx.manage.controller.manager;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.manager.ManagerLoginService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.manager.ManagerLogin;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 系统访问记录
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/manager/login")
public class ManagerLoginController extends BaseController {

    @Autowired
    private ManagerLoginService managerLoginService;


    @Permissions("manager:login:list")
    @GetMapping("/list.html")
    public String managerLogin() {
        return "manager/manager-login";
    }

    @Permissions("manager:login:list")
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        ManagerLogin managerLogin = new ManagerLogin();
        managerLogin.setIpAddr(request.getParameter("ipAddr"));
        managerLogin.setLoginName(request.getParameter("loginName"));
        managerLogin.setStatus(TKConvert.toInteger(request.getParameter("status")));
        List<ManagerLogin> list = managerLoginService.getList(managerLogin, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(managerLoginService.count(managerLogin));
        return rspData;
    }

    @Permissions("manager:login:remove")
    @Log(title = "登陆日志", businessType = BusinessType.DELETE)
    @PostMapping("/delete.json")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(managerLoginService.deleteByIds(ids));
    }

    @Permissions("manager:login:remove")
    @Log(title = "登陆日志", businessType = BusinessType.CLEAN)
    @PostMapping("/clean.json")
    @ResponseBody
    public AjaxResult clean() {
        managerLoginService.clean();
        return success();
    }

    @Permissions("manager:login:unlock")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @PostMapping("/unlock.json")
    @ResponseBody
    public AjaxResult unlock(String loginName) {
        //Cache<String, AtomicInteger> loginRecordCache = cacheManager.getCache(ShiroConstants.LOGIN_RECORD_CACHE);
        //if (loginRecordCache != null && loginRecordCache.size() > 0) {
        //    loginRecordCache.remove(loginName);
        //}
        return success();
    }
}
